package com.juc;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tgy
 * @Date: 2020-07-27 16:26
 */
public class SemaphoreTest {

    public static void main(String[] args) {

        /**
         * 类似停车场，最多进10辆车，后面出一辆车，进一辆车
         */
        Semaphore semaphore = new Semaphore(10);



        for (int i = 0; i < 100; i++) {

            final int j = i;
            new Thread(()->{

                try {


                    semaphore.acquire();
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("index = " + j);
                    semaphore.release();

                }catch (Exception e){

                }

            }).start();
        }


    }
}
